package com.zhn.usermanager.service.impl;

import com.zhn.usermanager.mapper.UserMapper;
import com.zhn.usermanager.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.FormatFlagsConversionMismatchException;
import java.util.HashMap;
import java.util.Map;

@Service
public class SendServiceImpl {
    @Resource
    private UserMapper userMapper;

    public Map<String, Object> sendCode(String phone) {

        String host = "https://intlsms.market.alicloudapi.com";
        String path = "/comms/sms/sendmsgall";
        String method = "POST";
        String appcode = "4a44804bd7e144138ee11876b5a46bf1";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        int phoneCode = (int) ((Math.random() * 9 + 1) * 100000);   // 生成6位验证码

//        String phoneCode = UUID.randomUUID().toString().substring(0, 6).replace("-", "");

//        System.out.println(phoneCode);

        bodys.put("callbackUrl", "http://test.dev.esandcloud.com");
        bodys.put("channel", "0");
        bodys.put("mobile", "+86" + phone);
        bodys.put("templateID", "0000000");
        bodys.put("templateParamSet", phoneCode + ", 5");

        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "发送失败");


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
//            System.out.println(EntityUtils.toString(response.getEntity()));
            String string = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = new JSONObject(string);
            // 解析返回的json对象，判断是否发送成功，如果发送成功，则更新当前用户表中的验证码内容
            // code为0000时，代表发送成功
            String code = jsonObject.getString("code");
            if("0000".equals(code)) {   // 四个0的字符串代表成功，如果成功，更新数据库用户验证码字段
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                System.out.println(format);
                userMapper.updateByPhone("" + phoneCode,format, phone);
                map.put("code", 200);
                map.put("msg", "发送成功");
            }
            System.out.println(phoneCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
